<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>spring定时任务 | du小站</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="spring定时任务使用的方式">
<meta property="og:type" content="article">
<meta property="og:title" content="spring定时任务">
<meta property="og:url" content="https://wenjies.github.io/2016/05/01/spring%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/index.html">
<meta property="og:site_name" content="du小站">
<meta property="og:description" content="spring定时任务使用的方式">
<meta property="og:locale">
<meta property="article:published_time" content="2016-04-30T16:00:00.000Z">
<meta property="article:modified_time" content="2024-12-02T02:10:27.128Z">
<meta property="article:author" content="杰">
<meta property="article:tag" content="定时任务">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="du小站" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 7.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">du小站</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">我们笑着说再见，却深知再见遥遥无期。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Suche"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://wenjies.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-spring定时任务" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2016/05/01/spring%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" class="article-date">
  <time class="dt-published" datetime="2016-04-30T16:00:00.000Z" itemprop="datePublished">2016-05-01</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Spring/">Spring</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      spring定时任务
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h4 id="spring-定时任务实现3中方式-推荐第二种"><a href="#spring-定时任务实现3中方式-推荐第二种" class="headerlink" title="spring 定时任务实现3中方式 推荐第二种"></a>spring 定时任务实现3中方式 推荐第二种</h4><h6 id="1-在spring-xml中定义好-scheduler，在容器中的bean-方法上加上-Scheduled注解"><a href="#1-在spring-xml中定义好-scheduler，在容器中的bean-方法上加上-Scheduled注解" class="headerlink" title="1.在spring xml中定义好 scheduler，在容器中的bean 方法上加上@Scheduled注解"></a>1.在spring xml中定义好 scheduler，在容器中的bean 方法上加上@Scheduled注解</h6><pre><code>xmlns:task=&quot;http://www.springframework.org/schema/task&quot;

http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd

&lt;task:annotation-driven /&gt;
&lt;task:scheduled-tasks scheduler=&quot;myScheduler&quot;&gt;  
    &lt;task:scheduled ref=&quot;scheduledTaskManager&quot; method=&quot;autoCardCalculate&quot; cron=&quot;1/5 * * * * *&quot;/&gt;  
&lt;/task:scheduled-tasks&gt;  
&lt;task:scheduler id=&quot;myScheduler&quot; pool-size=&quot;10&quot;/&gt;  

@Scheduled(cron = &quot;0/5 * * * * *&quot; )
public void doSomething() &#123;
    System.err.println(Thread.currentThread().getName() + &quot;   doSomething statrt.................&quot;);
&#125;
</code></pre>
<h6 id="2-使用注解启用-schedul，同样可以在容器中的bean-方法上加上-Scheduled注解来标识是调度，或者手动注册"><a href="#2-使用注解启用-schedul，同样可以在容器中的bean-方法上加上-Scheduled注解来标识是调度，或者手动注册" class="headerlink" title="2.使用注解启用 schedul，同样可以在容器中的bean 方法上加上@Scheduled注解来标识是调度，或者手动注册"></a>2.使用注解启用 schedul，同样可以在容器中的bean 方法上加上@Scheduled注解来标识是调度，或者手动注册</h6><pre><code>　
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer &#123;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) &#123;
        taskRegistrar.setScheduler(taskScheduler());
        taskRegistrar.addTriggerTask(myTask(), new CustomTrigger(&quot;0/5 * * * * *&quot;));
    &#125;

    @Bean(destroyMethod = &quot;shutdown&quot;)
    public Executor taskScheduler() &#123;
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
        // Executor executor=new ThreadPoolExecutor(4, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue&lt;Runnable&gt;(10000));
        return scheduledThreadPoolExecutor;
    &#125;

    @Bean
    public MyTask myTask() &#123;
        return new MyTask();
    &#125;
    
    @Scheduled(cron = &quot;0/5 * * * * *&quot; )
    public void doSomething() &#123;
        System.err.println(Thread.currentThread().getName() + &quot;   doSomething statrt.................&quot;);
    &#125;
&#125;

class MyTask implements Runnable &#123;
    @Override
    public void run() &#123;
        System.err.println(Thread.currentThread().getName() + &quot;   MyTask statrt.................&quot;);
        try &#123;
            Thread.sleep(10000);
        &#125; catch (InterruptedException e) &#123;
            // TODO Auto-generated catch block
            e.printStackTrace();
        &#125;
        System.err.println(Thread.currentThread().getName() + &quot;   MyTask end.................&quot;);
    &#125;
&#125;

class CustomTrigger implements Trigger &#123;

    private String cron;

    CustomTrigger(String cron) &#123;
        this.cron = cron;
    &#125;

    @Override
    public Date nextExecutionTime(TriggerContext triggerContext) &#123;
        CronTrigger trigger = new CronTrigger(cron);
        Date nextExec = trigger.nextExecutionTime(triggerContext);
        return nextExec;
    &#125;
&#125;
</code></pre>
<h6 id="3-使用-quartz-在spring-中代码-如下"><a href="#3-使用-quartz-在spring-中代码-如下" class="headerlink" title="3.使用 quartz 在spring 中代码 如下"></a>3.使用 quartz 在spring 中代码 如下</h6><pre><code>&lt;bean id=&quot;payTask&quot; class=&quot;cn.bookingsmart.jp.task.OrderTask&quot;/&gt;
&lt;!-- 支付超时 --&gt;
&lt;bean id=&quot;handlePayTimeout&quot; class=&quot;org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean&quot;&gt;
    &lt;property name=&quot;targetObject&quot;&gt;
        &lt;ref bean=&quot;payTask&quot; /&gt;
    &lt;/property&gt;
    &lt;property name=&quot;targetMethod&quot;&gt;
        &lt;value&gt;handlePayTimeout&lt;/value&gt;
    &lt;/property&gt;
    &lt;property name=&quot;concurrent&quot; value=&quot;false&quot; /&gt;
&lt;/bean&gt;

&lt;bean id=&quot;handlePayTimeoutTrigger&quot; class=&quot;org.springframework.scheduling.quartz.CronTriggerFactoryBean&quot;&gt;
    &lt;property name=&quot;jobDetail&quot;&gt;
        &lt;ref bean=&quot;handlePayTimeout&quot; /&gt;
    &lt;/property&gt;
    &lt;property name=&quot;cronExpression&quot;&gt;
        &lt;value&gt;0 0/5 * * * ? *&lt;/value&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean class=&quot;org.springframework.scheduling.quartz.SchedulerFactoryBean&quot;&gt;
    &lt;property name=&quot;triggers&quot;&gt;
        &lt;list&gt;
            &lt;ref bean=&quot;handlePayTimeoutTrigger&quot; /&gt;
            &lt;ref bean=&quot;handlePayErrTrigger&quot; /&gt;
            &lt;ref bean=&quot;handleTicketTrigger&quot; /&gt;
        &lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;bean id=&quot;threadPoolTaskExecutor&quot; class=&quot;org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor&quot;&gt;
  &lt;!-- 核心线程数，默认为1 --&gt;
  &lt;property name=&quot;corePoolSize&quot; value=&quot;3&quot; /&gt;
  &lt;!-- 最大线程数，默认为Integer.MAX_VALUE --&gt;
  &lt;property name=&quot;maxPoolSize&quot; value=&quot;10&quot; /&gt;
  &lt;!-- 队列最大长度，一般需要设置值&gt;=notifyScheduledMainExecutor.maxNum；默认为Integer.MAX_VALUE --&gt;
  &lt;property name=&quot;queueCapacity&quot; value=&quot;1000&quot; /&gt;
  &lt;!-- 线程池维护线程所允许的空闲时间，默认为60s --&gt;
  &lt;property name=&quot;keepAliveSeconds&quot; value=&quot;300&quot; /&gt;
  &lt;!-- 线程池对拒绝任务（无线程可用）的处理策略，目前只支持AbortPolicy、CallerRunsPolicy；默认为后者 --&gt;
  &lt;property name=&quot;rejectedExecutionHandler&quot;&gt;
      &lt;!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 --&gt;
      &lt;!-- CallerRunsPolicy:主线程直接执行该任务，执行完之后尝试添加下一个任务到线程池中，可以有效降低向线程池内添加任务的速度 --&gt;
      &lt;!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持；会导致被丢弃的任务无法再次被执行 --&gt;
      &lt;!-- DiscardPolicy:抛弃当前任务、暂不支持；会导致被丢弃的任务无法再次被执行 --&gt;
      &lt;bean class=&quot;java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy&quot; /&gt;
  &lt;/property&gt;
&lt;/bean&gt;


public class OrderTask &#123;
    @Resource
    private OrderService orderService;
    @Resource
    private QaeService qaeService;

    public void handlePayTimeout() &#123;
     
    &#125;
    
    public void handlePayErr() &#123;
         
    &#125;
    
    public void handNoTicket() &#123;
         
    &#125;

&#125;
</code></pre>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://wenjies.github.io/2016/05/01/spring%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" data-id="cm3zaz9c90018tgu66ki1awf3" data-title="spring定时任务" class="article-share-link"><span class="fa fa-share">Teilen</span></a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" rel="tag">定时任务</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/08/01/mysql%E7%9A%84%E7%B4%A2%E5%BC%95/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          mysql的索引
        
      </div>
    </a>
  
  
    <a href="/2016/03/15/java%E4%B8%AD%E5%BC%BA%E5%BC%95%E7%94%A8%EF%BC%8C%E8%BD%AF%E5%BC%95%E7%94%A8%EF%BC%8C%E5%BC%B1%E5%BC%95%E7%94%A8%EF%BC%8C%E8%99%9A%E5%BC%95%E7%94%A8%E6%A6%82%E5%BF%B5%E6%AF%94%E8%BE%83/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">java中强引用，软引用，弱引用，虚引用概念比较</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Kategorien</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/AJAX/">AJAX</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA/">JAVA</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/MQ/">MQ</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Mybatis/">Mybatis</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Redis/">Redis</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Spring/">Spring</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/">分布式事务</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%8F%E9%AA%8C/">经验</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/" rel="tag">aop</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/bean/" rel="tag">bean</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/micrometer/" rel="tag">micrometer</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mvc/" rel="tag">mvc</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/" rel="tag">mybatis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rocketmq/" rel="tag">rocketmq</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/" rel="tag">spring</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/" rel="tag">zookeeper</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%8B%E5%8A%A1/" rel="tag">事务</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/" rel="tag">分布式事务</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" rel="tag">单元测试</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" rel="tag">定时任务</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%89%A9%E5%B1%95/" rel="tag">扩展</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%B4%E5%90%88/" rel="tag">整合</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%9D%A2%E8%AF%95/" rel="tag">面试</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 10px;">aop</a> <a href="/tags/bean/" style="font-size: 10px;">bean</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/java/" style="font-size: 10px;">java</a> <a href="/tags/js/" style="font-size: 10px;">js</a> <a href="/tags/micrometer/" style="font-size: 10px;">micrometer</a> <a href="/tags/mvc/" style="font-size: 13.33px;">mvc</a> <a href="/tags/mybatis/" style="font-size: 10px;">mybatis</a> <a href="/tags/mysql/" style="font-size: 20px;">mysql</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/rocketmq/" style="font-size: 10px;">rocketmq</a> <a href="/tags/spring/" style="font-size: 10px;">spring</a> <a href="/tags/zookeeper/" style="font-size: 10px;">zookeeper</a> <a href="/tags/%E4%BA%8B%E5%8A%A1/" style="font-size: 10px;">事务</a> <a href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/" style="font-size: 10px;">分布式事务</a> <a href="/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" style="font-size: 10px;">单元测试</a> <a href="/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 16.67px;">基础知识</a> <a href="/tags/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" style="font-size: 10px;">定时任务</a> <a href="/tags/%E6%89%A9%E5%B1%95/" style="font-size: 10px;">扩展</a> <a href="/tags/%E6%95%B4%E5%90%88/" style="font-size: 10px;">整合</a> <a href="/tags/%E9%9D%A2%E8%AF%95/" style="font-size: 10px;">面试</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2025/01/">January 2025</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2024/11/">November 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">October 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">September 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">July 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">May 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">December 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">November 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">October 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">August 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/">May 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">March 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">February 2016</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2025/01/10/spring%20boot3%E6%95%B4%E5%90%88ELK%E5%AE%9E%E7%8E%B0%E6%97%A5%E5%BF%97%E6%90%9C%E7%B4%A2/">spring boot3整合ELK实现日志搜索</a>
          </li>
        
          <li>
            <a href="/2025/01/10/spring%20boot3%E6%95%B4%E5%90%88%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AA/">spring boot3整合链路追踪</a>
          </li>
        
          <li>
            <a href="/2024/11/27/hexo%E4%BD%BF%E7%94%A8/">hexo使用</a>
          </li>
        
          <li>
            <a href="/2017/10/17/spring%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/">spring单元测试</a>
          </li>
        
          <li>
            <a href="/2017/09/16/jsonp%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82/">jsonp跨域请求</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2025 杰<br>
      <a href="https://beian.miit.gov.cn/" target="_blank">蜀ICP备19041392号-1</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>